package com.xudu.tourbackend.controller.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xudu.tourbackend.common.DeleteBatchRequest;
import com.xudu.tourbackend.common.DeleteRequest;
import com.xudu.tourbackend.common.PageRequest;
import com.xudu.tourbackend.common.Result;
import com.xudu.tourbackend.common.globalEnum.ErrorCode;
import com.xudu.tourbackend.controller.ScenicAreaController;
import com.xudu.tourbackend.entity.ScenicArea;
import com.xudu.tourbackend.entity.ScenicSpot;
import com.xudu.tourbackend.entity.request.AddScenicAreaRequest;
import com.xudu.tourbackend.entity.request.AddScenicSpotRequest;
import com.xudu.tourbackend.entity.request.SearchScenicAreaRequest;
import com.xudu.tourbackend.entity.request.UpdateScenicAreaRequest;
import com.xudu.tourbackend.entity.vo.RouteVo;
import com.xudu.tourbackend.entity.vo.ScenicAreaVo;
import com.xudu.tourbackend.exception.ServiceException;
import com.xudu.tourbackend.mapper.ScenicAreaMapper;
import com.xudu.tourbackend.mapper.ScenicSpotMapper;
import com.xudu.tourbackend.service.IScenicAreaService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.Collections;
import java.util.List;

/**
 * @className: ScenicAreaControllerImpl
 * @description: 景区操作方法的实现
 * @author: xudu
 * @create: 2024-06-17
 */
@CrossOrigin(origins = "*")
@RestController
@Slf4j
public class ScenicAreaControllerImpl implements ScenicAreaController {


   @Resource
   private IScenicAreaService scenicAreaService;


    @Override
    public Result addScenicArea(@Valid AddScenicAreaRequest addScenicAreaRequest, HttpServletRequest request) {
        if (addScenicAreaRequest == null){
            throw new ServiceException(ErrorCode.NULL_ERROR);
        }
        int i = scenicAreaService.addScenicArea(addScenicAreaRequest, request);
        return Result.success(i);
    }

    @Override
    public Result searchScenicArea(SearchScenicAreaRequest searchScenicAreaRequest) {
        if (searchScenicAreaRequest == null){
            throw new ServiceException(ErrorCode.NULL_ERROR);
        }

        IPage<ScenicAreaVo> scenicAreaVoIPage = scenicAreaService.searchScenicArea(searchScenicAreaRequest);

        //分页
        //PageRequest pageRequest = new PageRequest();
        //PageHelper.startPage(pageRequest.getPageNum(), pageRequest.getPageSize());
        //PageInfo<ScenicAreaVo> pageInfo = new PageInfo<>(scenicAreaVos);

        return Result.success(scenicAreaVoIPage);
    }


    @Override
    public Result updateScenicArea(@Valid UpdateScenicAreaRequest updateScenicAreaRequest, HttpServletRequest request) {
        Boolean b = scenicAreaService.updateScenicArea(updateScenicAreaRequest, request);
        if (!b){
            throw new ServiceException(ErrorCode.SYSTEM_ERROR);
        }
        return Result.success(b);
    }

    @Override
    public Result deleteScenicArea(@RequestBody DeleteBatchRequest deleteBatchRequest, HttpServletRequest request) {
        List<Integer> ids = deleteBatchRequest.getIds();
        if (ids.isEmpty()){
            throw new ServiceException(ErrorCode.PARAMS_ERROR);
        }
        int i = scenicAreaService.deleteScenicArea(ids, request);
        return Result.success(i);

    }
}
